Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
Some DDE links do not include an item. In this case, only the application and topic are returned, separated by a vertical pipe character. Once you have the DDE link information, you can use Visual Basic’s string manipulation functions to extract individual strings for the application, topic, and item (if present). Next, follow this procedure to establish the link: 1. Set the destination control’s LinkMode property to 0-None. 2. Set the LinkTopic property to the application and topic retrieved from the Clipboard. 3. If necessary, set the LinkItem property using the item obtained from the Clipboard. 4. Set the destination control’s LinkMode property to 1-Automatic. You can see that the steps required to establish a DDE link with your Visual Basic application as the destination are relatively simple using the Clipboard. It’s essential, however, that your program verifies that the operation is valid before attempting to establish the link. Screening out invalid links before they have been established can save head-aches later. Two conditions must be satisfied for a Paste Link operation to be valid. First, DDE link information must be available on the Clipboard. Second, the data available on the Clipboard must be in a format that is appropriate for the intended destination control. For example, you can link text data—but not a graphic—to a Text Box control. Similarly, you can link a graphic—but not text—to a Picture Box control. The program needs to verify that both of these conditions are met before attempting a Paste Link. One way to accomplish this is to enable or disable the Paste Link menu command, depending on whether these conditions are met. ### Making Paste Link Work For You Now you’re ready to apply some of what you have been learning. In this section, you’ll develop a simple program that can act as the destination for a Paste Link command. To make things more interesting, the program will provide two possible destination controls—a Text Box and a Picture Box—so you can link both text and graphics. You’ll also see how to enable or disable the Paste Link command, depending on whether appropriate data is on the Clipboard. The program’s single form is quite simple, containing only three controls: a Picture Box on the left side of the form, a Text Box on the right side, and a Command Button at the bottom. All of the control properties can be left at their default values except for the Text Box’s MultiLine property (which should be changed to True), and the Command Button’s Caption property (which should be changed to Exit ). Next, press Ctrl+E to bring up the Menu Editor. Using the techniques you learned in Chapter 3, create an Edit menu, named mnuEdit, and add to that menu a single item with the caption Paste Link and the name mnuPasteLink. The form is now complete. Now let’s work on the code. The Click event procedure for the Command Button is trivial, containing only the End statement. You also need to define a couple of constants in the general declarations section of the form, as shown here:
    Const NONE = 0, AUTOMATIC = 1
The program’s main functionality resides in event procedures for the menu commands. When the menu is displayed, you want to enable the Paste Link command only if the Clipboard contains both DDE link information and data in a format that is appropriate for the control that currently has the focus (text data for the Text Box, a graphic for the Picture Box). You accomplish this in the Click event procedure for the mnuEdit menu command. Code in this procedure first disables the menu command; it is enabled only if the required conditions are met. Begin by using the Clipboard object’s GetFormat method with the vbCFLink argument to determine whether the Clipboard contains DDE link information. If not, exit the sub, leaving the Paste Link command disabled (remember that the Clipboard can contain data without link information). If the link information is present, determine whether the type of data matches the current control. Because the Clipboard can hold three types of graphics, you have to check for all three. This check is accomplished by these lines of code:
    X = Clipboard.GetFormat(vbCFBitmap)
    X = X Or Clipboard.GetFormat(vbCFMetafile)
    X = X Or Clipboard.GetFormat(vbCFDIB)
The first line sets X to True if the Clipboard contains a bitmap graphic. Then X is Or ed in turn with the result of the questions: “Does the Clipboard contain a metafile graphic?” and “Does the Clipboard contain a device-independent bitmap graphic?” The result is that X is True if the Clipboard contains any one of the three graphics formats, and False otherwise. But how do you determine which of the form’s controls currently has the focus? First, you need to determine the identity of the control with the focus. You do so with the ActiveControl property, which returns the control that has the focus. The ActiveControl property applies to the Screen object as well as to Form and MDIForm objects. This is a useful property, permitting you to access and manipulate the active control, no matter which control it is. You will use the Screen object’s ActiveControl property, which returns the control that has the focus on the active form. Note, however, that you can use the Form or MDIForm object’s ActiveControl property to access the control that has the focus on a form—even when the form is not active. Once you have retrieved the control that has the focus, how do you determine which type of control it is? You use the TypeOf...Is statement, which has the following syntax:
    TypeOf objectName Is objectType
In this syntax, objectName is any Visual Basic object. It can be a form or control name, but more often the control returned by the ActiveControl property is used here. The objectType is the identifier for any Visual Basic control object—that is, any control. For a list of the identifiers associated with each Visual Basic control, search for “Object Type” in the Visual Basic Help system. Now you can put things together. The If statement that follows will execute only if the active control on the screen is a Picture Box:
    If TypeOf Screen.ActiveControl Is PictureBox Then
    ...
    End If
You can do the same for a Text Box, ending up with the following code:
    X = Clipboard.GetFormat(vbCFBitmap)
    X = X Or Clipboard.GetFormat(vbCFMetafile)
    X = X Or Clipboard.GetFormat(vbCFDIB)

    If X And (TypeOf Screen.ActiveControl Is PictureBox) Then
        mnuEditPasteLink.Enabled = True
        Exit Sub
    End If
The effect of this code is: “If the Clipboard contains a graphic image and the control with the focus is a Picture Box, then enable the Paste Link command and exit the sub.” You mu